package hsit;

import java.io.File;
import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author huanggy
 * @date 2022/08/27
 **/
public class CountMain {

    public static void main(String[] args) {

        Class[] classes = getClassByPackage(CountMain.class.getPackage().getName());
        Map<String, Integer> interfaceMethodMap = new HashMap<>();
        for (Class aClass : classes) {
            interfaceMethodMap.put(aClass.getSimpleName(), aClass.getMethods().length);
        }

        interfaceMethodMap.keySet().forEach(key->{
            System.out.println(key + ":" + interfaceMethodMap.get(key) );
        });

    }

    public static Class[] getClassByPackage(String packageName) {
        try {
            Enumeration<URL> resources = CountMain.class.getClassLoader().getResources(packageName.replaceAll("\\.", "/"));
            while (resources.hasMoreElements()) {
                URL url = resources.nextElement();
                String[] file = new File(url.getFile()).list();
                List<String> fileList = Arrays.stream(file).filter(fileUrl -> fileUrl.endsWith(".class") && !fileUrl.contains("CountMain.class")).collect(Collectors.toList());
                Class[] classList = new Class[fileList.size()];

                for (int i = 0; i < fileList.size(); i++) {
                    classList[i] = Class.forName(packageName + "." + fileList.get(i).replaceAll("\\.class", ""));
                }
                return classList;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
